package com.atguigu.gulimall.gulimall.auth.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.gulimall.auth.feign.MemberFeignService;
import com.atguigu.gulimall.gulimall.auth.vo.RealSocialUesr;
import com.atguigu.gulimall.gulimall.auth.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.atguigu.common.vo.MemberResponseVo;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
public class OAuth2Controller {

    @Autowired
    MemberFeignService memberFeignService;

    @GetMapping(value = "/oauth2.0/gitee/success")
    public String gitee(@RequestParam("code") String code, HttpSession session) throws Exception {

        Map<String,String> map = new HashMap<>();
        map.put("client_id","b0ff6ad2ad4b8503e3c85e91f78c8275befd79a352ebfec0d83e6e4edd61fa4b");
        map.put("code",code);
        map.put("redirect_uri","http://auth.gulimall.com/oauth2.0/gitee/success");
        map.put("client_secret","b37b4c098b497c4a79dd2b2d5d4196d40ba71532350d547090edae3da78b2d89");
            HttpResponse post = HttpUtils.doPost("https://gitee.com", "/oauth/token?grant_type=authorization_code", "post",  new HashMap<>(), new HashMap<>(),map);
        if(post.getStatusLine().getStatusCode()==200){
            String s = EntityUtils.toString(post.getEntity());
            SocialUser socialUser = JSON.parseObject(s, SocialUser.class);
            String access_token = socialUser.getAccess_token();
            Map<String,String> map2=new HashMap<String, String>();
            map2.put("access_token",access_token);
            HttpResponse httpResponse = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "get", new HashMap<String, String>(),map2);
            String json = EntityUtils.toString(httpResponse.getEntity());
            RealSocialUesr realSocialUesr = JSON.parseObject(json, RealSocialUesr.class);
            socialUser.setUid(String.valueOf(realSocialUesr.getId()));
            R r = memberFeignService.oauthLogin(socialUser);
            if(r.getCode()==0){
                MemberResponseVo data = r.getData("data", new TypeReference<MemberResponseVo>() {
                });
                log.info("登录成功，用户信息",data.toString());
                session.setAttribute(AuthServerConstant.LOGIN_USER,data);
                return "redirect:http://gulimall.com";
            }
            else {
                return "redirect:http://auth.gulimall.com/login.html";
            }

        }
        else {
            return "redirect:http://auth.gulimall.com/login.html";
        }


    }
}
